//1.引入express
const express = require('express');

//2.创建应用对象
const app = express();

//3.创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装
app.get('/server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  //设置响应
  response.send('HELLO EXPRESS get请求')
})

app.post('/server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  //设置响应
  response.send('HELLO EXPRESS POST请求')
})

//针对IE缓存
app.get('/ie', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  //设置响应
  response.send('针对IE缓存 ,GET请求-33')
})

//延时响应
app.get('/delay', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.setHeader('Access-Control-Allow-Headers', '*')
  setTimeout(() => {
    response.send('延时3秒响应')
  }, 3000)

})


//jquery服务
app.all('/jquery-server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    title: '立志成为高级前端开发--->全栈工程师',
    cur: 'jquery'
  }
  // setTimeout(() => {
  response.send(JSON.stringify(data))
  // }, 3000)
})

//axios 服务
app.all('/axios-server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    title: '立志成为高级前端开发--->全栈工程师',
    cur: 'axios'
  }
  // setTimeout(() => {
  response.send(JSON.stringify(data))
  // }, 3000)
})

//fetch 服务
app.all('/fetch-server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    title: '立志成为高级前端开发--->全栈工程师',
    cur: 'fetch'
  }
  // setTimeout(() => {
  response.send(JSON.stringify(data))
  // }, 3000)
})

//jsonp 服务
app.all('/jsonp-server', (request, response) => {
  //设置响应头，设置允许跨域
  // response.setHeader('Access-Control-Allow-Origin', '*')
  // response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    name: '立志成为高级前端开发--->全栈工程师',
    cur: 'jsonp'
  }
  const str = JSON.stringify(data)
  response.end(`handle(${str})`)
  // response.send('console.log("HELLO JSONP SERVER")')

})


//用户名检测是否存在
app.all('/check-username', (request, response) => {
  //设置响应头，设置允许跨域
  // response.setHeader('Access-Control-Allow-Origin', '*')
  // response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    exit: '1',
    msg: '用户名已经存在'
  }
  const str = JSON.stringify(data)
  response.end(`handle(${str})`)
  // response.send('console.log("HELLO JSONP SERVER")')

})

//jQuery-jsonp的方法
app.all('/jquery-jsonp-server', (request, response) => {
  //设置响应头，设置允许跨域
  // response.setHeader('Access-Control-Allow-Origin', '*')
  // response.setHeader('Access-Control-Allow-Headers', '*')
  const data = {
    name: 'jijiking',
    arr: ['吉吉国王', '熊大', '🐻熊二', '🧑🏻‍🦲光头强']
  }
  const str = JSON.stringify(data)
  const cb = request.query.callback
  // response.end(`handle(${str})`)
  response.end(`${cb}(${str})`)
  // response.send('console.log("HELLO JSONP SERVER")')

})

//cors
app.all('/cors-server', (request, response) => {
  //设置响应头，可以实现跨域
  response.setHeader("Access-Control-Allow-Origin", "*")
  // response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5000")
  response.setHeader("Access-Control-Allow-Headers", "*")
  response.setHeader("Access-Control-Allow-Method", "*")

  response.send('HELLO CORS')
})




app.all('/json-server', (request, response) => {
  //设置响应头，设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.setHeader('Access-Control-Allow-Headers', '*')
  //响应一个数据
  const data = {
    name: 'jiqirui'
  }
  //对对象进行字符串转换
  let str = JSON.stringify(data)
  //设置响应体
  //send中只能是字符串类型
  // response.send('HELLO EXPRESS All请求')
  response.send(str)
})

//监听端口启动服务
app.listen(8000, () => {
  console.log('服务已经启动，8000端口监听中...');
})